#include <iostream>

#include "array.h"

using namespace std;

ostream &operator <<(ostream &os, const Array &array)
{
	os << "{";
	for (int i = 0; i < array.length(); i++)
		os << array.buffer[i] << (i < array.length()-1 ? ", " : "");
	os << "}";
	return os;
}

Array::Array(int size, int *arr) :
   size(size),
   buffer(new int[size])
{
	for (int i = 0; i < size; i++)
		buffer[i] = arr ? arr[i] : 0;
   cout << "constructor. size: " << size << endl;
}

Array::~Array()
{
   cout << "destructor. size: " << size << endl;
   delete[] buffer;
}

Array& Array::operator= (const Array & other)
{
   if (this != &other)
   {
      delete[] buffer;
      cout << "delete[]. size: " << size << endl;
      buffer = new int[other.size];
      cout << "new[]. size: " << other.size << endl;
      size = other.size;
      for (int i = 0; i < size; i++)
         buffer[i] = other.buffer[i];
   }
   return *this;
}

ostream &operator <<(ostream &os, const ArrayIterator &array) {
	os << array.current;
	return os;
}

ArrayIterator ArrayIterator::operator ++()
{
	if (current == beyondLast)
      throw ArrayIndexOutOfBounds("Failed to increment iterator beyond last array element");
	current++;
	return *this;
}
